from common.database import db
from model.user import User
import json


class DetectResult(db.Model):
    __tablename__ = 'detect_result'
    uuid = db.Column(db.String, primary_key=True, nullable=False)
    user_email = db.Column(db.String, db.ForeignKey('user.email'), nullable=False)
    detect_time = db.Column(db.DateTime, nullable=False)
    upload_path = db.Column(db.String, nullable=False)
    result_path = db.Column(db.String, nullable=False)
    speed = db.Column(db.Double, nullable=False)
    imgsz = db.Column(db.Integer, nullable=False)
    conf = db.Column(db.Float, nullable=False)
    iou = db.Column(db.Float, nullable=False)
    max_det = db.Column(db.Integer, nullable=False)
    augment = db.Column(db.Boolean, nullable=False)
    agnostic_nms = db.Column(db.Boolean, nullable=False)
    retina_masks = db.Column(db.Boolean, nullable=False)
    classes = db.Column(db.String, nullable=False)
    batch = db.Column(db.Integer, nullable=True)
    vid_stride = db.Column(db.Integer, nullable=True)
    user = db.relationship('User', backref='detect_result', lazy=True)

    def to_dict(self):
        return {
            'uuid': self.uuid,
            'user_email': self.user_email,
            'user_name': self.user.name,
            'detect_time': self.detect_time,
            'upload_path': self.upload_path,
            'result_path': self.result_path,
            'speed': self.speed,
            'imgsz': self.imgsz,
            'conf': self.conf,
            'iou': self.iou,
            'max_det': self.max_det,
            'augment': self.augment,
            'agnostic_nms': self.agnostic_nms,
            'retina_masks': self.retina_masks,
            'classes': self.classes,
            'batch': self.batch,
            'vid_stride': self.vid_stride
        }
